//
//  SETimer.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/8.
//

import UIKit

class SETimer: NSObject {
    private(set) weak var timer: Timer!
    
    class func scheduledTimer(with timeInterval: TimeInterval, repeats: Bool, block: @escaping (_ timer: Timer) -> Void) -> SETimer {
        let timer = SETimer()
        if timer.timer == nil {
            timer.timer = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: repeats, block: block)
            RunLoop.current.add(timer.timer, forMode: .common)
        }
        return timer
    }
    
    func fire() { timer.fire() }
    
    func invalidate() {
        if timer != nil {
            timer.invalidate()
            timer = nil
        }
    }
    
    deinit {
        SELog("timer已销毁")
        invalidate()
    }
}
